package org.springblade.modules.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springblade.modules.platform.dto.RuleLinkageDTO;
import org.springblade.modules.platform.dto.RuleLinkagePageDTO;
import org.springblade.modules.platform.dto.RuleLinkageUpdateDTO;
import org.springblade.modules.platform.entity.RuleLinkage;
import org.springblade.modules.platform.entity.RuleLinkageMeasuring;
import org.springblade.modules.platform.mapper.DeviceTableMapper;
import org.springblade.modules.platform.mapper.RuleLinkageMapper;
import org.springblade.modules.platform.mapper.RuleLinkageMeasuringMapper;
import org.springblade.modules.platform.service.IRuleLinkageService;
import org.springblade.modules.platform.vo.RuleLinkageListByPageVO;
import org.springblade.modules.platform.vo.RuleLinkageVO;
import org.springblade.modules.platform.vo.enums.IsIssueEnum;
import org.springblade.modules.platform.vo.enums.RuleLinkageEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SYG
 * @since 2022-08-30
 */
@Service
public class RuleLinkageServiceImpl extends ServiceImpl<RuleLinkageMapper, RuleLinkage> implements IRuleLinkageService {

    @Autowired
    RuleLinkageMeasuringMapper ruleLinkageMeasuringMapper;
    @Autowired
    DeviceTableMapper deviceTableMapper;

    @Override
    public RuleLinkageListByPageVO selectRuleLinkageList(RuleLinkagePageDTO ruleLinkagePageDTO) {
        RuleLinkageListByPageVO ruleLinkageListByPageVO = new RuleLinkageListByPageVO();
        QueryWrapper<RuleLinkage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RuleLinkage::getIsDeleted,0);
        if(!StringUtils.isEmpty(ruleLinkagePageDTO.getRuleLinkageName())){
            queryWrapper.lambda().like(RuleLinkage::getRuleLinkageName,ruleLinkagePageDTO.getRuleLinkageName());
        }
        Page<RuleLinkage> ruleLinkagePage = baseMapper.selectPage(new Page<>(ruleLinkagePageDTO.getCurrent(), ruleLinkagePageDTO.getSize()), queryWrapper);
        ruleLinkageListByPageVO.setTotal(ruleLinkagePage.getTotal());
        if(ruleLinkagePage.getTotal()>0){
            List<RuleLinkage> records = ruleLinkagePage.getRecords();
            List<RuleLinkageVO> collect = records.stream().map(x -> {
                RuleLinkageVO ruleLinkageVO = new RuleLinkageVO();
                BeanUtils.copyProperties(x, ruleLinkageVO);
                ruleLinkageVO.setIsIssue(IsIssueEnum.getEnum(x.getIsIssue()));
                ruleLinkageVO.setState(RuleLinkageEnum.getEnum(x.getState()));
                return ruleLinkageVO;
            }).collect(Collectors.toList());
            ruleLinkageListByPageVO.setLinkageVOS(collect);
        }
        return ruleLinkageListByPageVO;
    }

    @Override
    public RuleLinkageVO selectRuleLinkageById(Integer id) {
        RuleLinkage ruleLinkage = baseMapper.selectById(id);
        RuleLinkageVO ruleLinkageVO = new RuleLinkageVO();
        BeanUtils.copyProperties(ruleLinkage, ruleLinkageVO);
        ruleLinkageVO.setIsIssue(IsIssueEnum.getEnum(ruleLinkage.getIsIssue()));
        ruleLinkageVO.setState(RuleLinkageEnum.getEnum(ruleLinkage.getState()));
        ruleLinkageVO.setPressureScope(ruleLinkage.getPressureScope());

        List<RuleLinkageMeasuring> ruleLinkageDevices = ruleLinkageMeasuringMapper.selectList(Wrappers.<RuleLinkageMeasuring>query().lambda()
                .eq(RuleLinkageMeasuring::getRuleLinkageId, ruleLinkage.getId()));
        if(!CollectionUtils.isEmpty(ruleLinkageDevices)){
            List<RuleLinkageVO.RuleLinkageMeasuringVO> collect = ruleLinkageDevices.stream().map(x -> {
                RuleLinkageVO.RuleLinkageMeasuringVO ruleLinkageDeviceVO = new RuleLinkageVO.RuleLinkageMeasuringVO();
                ruleLinkageDeviceVO.setMeasuringId(x.getMeasuringId());
                ruleLinkageDeviceVO.setMeasuringName(x.getMeasuringName());

                return ruleLinkageDeviceVO;
            }).collect(Collectors.toList());
            ruleLinkageVO.setRuleLinkageMeasuringDTOS(collect);
        }
        return ruleLinkageVO;
    }

    @Override
    public void instartRuleLinkage(RuleLinkageDTO ruleLinkageDTO) {
        RuleLinkage ruleLinkage = new RuleLinkage();
        List<RuleLinkageDTO.RuleLinkageMeasuringDTO> ruleLinkageDeviceDTOS = ruleLinkageDTO.getRuleLinkageMeasuringDTOS();
        ruleLinkage.setRuleLinkageName(ruleLinkageDTO.getRuleLinkageName());
        ruleLinkage.setDeviceId(ruleLinkageDTO.getDeviceId());
        ruleLinkage.setCronExpression(ruleLinkageDTO.getCronExpression());
        ruleLinkage.setInsertTime(System.currentTimeMillis());
        ruleLinkage.setUpdateTime(System.currentTimeMillis());
        ruleLinkage.setIsDeleted(0);
        ruleLinkage.setPressure(ruleLinkageDTO.getPressure());
        ruleLinkage.setState(RuleLinkageEnum.CLOSURE.getCode());
        ruleLinkage.setDeviceNumber(ruleLinkageDTO.getDeviceNumber());
        ruleLinkage.setPressureScope(ruleLinkageDTO.getPressureScope());
        baseMapper.insert(ruleLinkage);

        if(!CollectionUtils.isEmpty(ruleLinkageDeviceDTOS)){
            ruleLinkageDeviceDTOS.forEach(x->{
                RuleLinkageMeasuring ruleLinkageDevice = new RuleLinkageMeasuring();
                ruleLinkageDevice.setRuleLinkageId(ruleLinkage.getId());
                ruleLinkageDevice.setMeasuringId(x.getMeasuringId());
                ruleLinkageDevice.setMeasuringName(x.getMeasuringName());
                ruleLinkageMeasuringMapper.insert(ruleLinkageDevice);
            });
        }
    }

    @Override
    public void deleteRuleLinkage(Integer[] id) {
        List<RuleLinkage> ruleLinkages = baseMapper.selectBatchIds(Lists.newArrayList(id));
        ruleLinkages.forEach(x->{
            x.setIsDeleted(1);
            x.setUpdateTime(System.currentTimeMillis());
            baseMapper.updateById(x);
        });
    }

    @Override
    public void updateRuleLinkage(RuleLinkageDTO ruleLinkageDTO) {
        RuleLinkage ruleLinkage = new RuleLinkage();
        ruleLinkage.setRuleLinkageName(ruleLinkageDTO.getRuleLinkageName());
        ruleLinkage.setDeviceId(ruleLinkageDTO.getDeviceId());
        ruleLinkage.setCronExpression(ruleLinkageDTO.getCronExpression());
        ruleLinkage.setUpdateTime(System.currentTimeMillis());
        ruleLinkage.setIsDeleted(0);
        ruleLinkage.setPressure(ruleLinkageDTO.getPressure());
        ruleLinkage.setPressureScope(ruleLinkageDTO.getPressureScope());
        ruleLinkage.setId(ruleLinkageDTO.getId());
        ruleLinkage.setState(RuleLinkageEnum.CLOSURE.getCode());
        ruleLinkage.setDeviceNumber(ruleLinkageDTO.getDeviceNumber());
        baseMapper.updateById(ruleLinkage);
        ruleLinkageMeasuringMapper.delete(Wrappers.<RuleLinkageMeasuring>query().lambda()
                .eq(RuleLinkageMeasuring::getRuleLinkageId, ruleLinkage.getId()));

        List<RuleLinkageDTO.RuleLinkageMeasuringDTO> ruleLinkageDeviceDTOS = ruleLinkageDTO.getRuleLinkageMeasuringDTOS();
        if(!CollectionUtils.isEmpty(ruleLinkageDeviceDTOS)){
            ruleLinkageDeviceDTOS.forEach(x->{
                RuleLinkageMeasuring ruleLinkageDevice = new RuleLinkageMeasuring();
                ruleLinkageDevice.setRuleLinkageId(ruleLinkage.getId());
                ruleLinkageDevice.setMeasuringId(x.getMeasuringId());
                ruleLinkageDevice.setMeasuringName(x.getMeasuringName());
                ruleLinkageMeasuringMapper.insert(ruleLinkageDevice);
            });
        }
    }

    @Override
    public void updateRuleLinkage(RuleLinkageUpdateDTO ruleLinkageUpdateDTO) {
        RuleLinkage ruleLinkage = baseMapper.selectById(ruleLinkageUpdateDTO.getId());
        ruleLinkage.setUpdateTime(System.currentTimeMillis());
/*        ruleLinkage.setIsIssue(ruleLinkageUpdateDTO.getIsIssue().getCode());*/
        ruleLinkage.setState(ruleLinkageUpdateDTO.getRuleLinkageState().getCode());
        baseMapper.updateById(ruleLinkage);
    }
}
